package com.bst.etl.controller;

import com.bst.common.core.domain.entity.SysDictData;
import com.bst.etl.utils.DDLUtil;
import com.bst.etl.vo.EtlLabelDataVO;
import com.bst.base.domain.BaseLabel;
import com.bst.common.annotation.Log;
import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.core.page.TableDataInfo;
import com.bst.common.enums.BusinessType;
import com.bst.common.utils.poi.ExcelUtil;
import com.bst.etl.domain.EtlDrawDw;
import com.bst.etl.service.IEtlDrawDwService;
import com.bst.etl.vo.TableCreatParamVO;
import com.bst.generator.domain.GenTableColumn;
import com.bst.md.domain.MdMedPubfld;
import com.bst.md.service.IMdMedPubfldService;
import com.bst.md.vo.TableFieldDiffVO;
import com.bst.md.vo.TableFieldVO;
import oracle.jdbc.proxy.annotation.Post;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.bst.base.service.IBaseLabelService;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 标准仓库Controller
 * 
 * @author ruoyi
 * @date 2024-02-28
 */
@Controller
@RequestMapping("/base/etl/dw")
public class EtlDrawDwController extends BaseController
{
    private String prefix = "etl/dw";

    @Autowired
    private IEtlDrawDwService etlDrawDwService;
    @Autowired
    private IBaseLabelService baseLabelService;

    @GetMapping()
    public String dw(ModelMap mmap)
    {
        BaseLabel label = new BaseLabel();
        label.setParentId(0L);
        label.setSdCls("dw");
        List<BaseLabel> labels = baseLabelService.selectBaseLabelList(label);
        List<BaseLabel> children;
        for (BaseLabel l : labels) {
            label.setParentId(l.getIdLabel());
            children = baseLabelService.selectBaseLabelList(label);
            l.setChildren(children);
        }
        mmap.put("labels",labels);
        return prefix + "/dw";
    }

    /**
     * 查询标准仓库列表
     */
    @PostMapping("/list2")
    @ResponseBody
    public TableDataInfo list2(EtlLabelDataVO dataVO)
    {
        startPage();
        List<EtlDrawDw> list = etlDrawDwService.selectEtlDrawDwList2(dataVO);
        return getDataTable(list);
    }

    /**
     * 导出标准仓库列表
     */
    @Log(title = "标准仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EtlDrawDw etlDrawDw)
    {
        List<EtlDrawDw> list = etlDrawDwService.selectEtlDrawDwList(etlDrawDw);
        ExcelUtil<EtlDrawDw> util = new ExcelUtil<EtlDrawDw>(EtlDrawDw.class);
        return util.exportExcel(list, "标准仓库数据");
    }

    /**
     * 新增标准仓库
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存标准仓库
     */
    @Log(title = "标准仓库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EtlDrawDw etlDrawDw)
    {
        return toAjax(etlDrawDwService.insertEtlDrawDw(etlDrawDw));
    }

    /**
     * 修改标准仓库
     */
    @GetMapping("/edit/{idDw}")
    public String edit(@PathVariable("idDw") Long idDw, ModelMap mmap)
    {
        EtlDrawDw etlDrawDw = etlDrawDwService.selectEtlDrawDwByIdDw(idDw);
        mmap.put("etlDrawDw", etlDrawDw);
        return prefix + "/edit";
    }

    /**
     * 修改保存标准仓库
     */
    @Log(title = "标准仓库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EtlDrawDw etlDrawDw)
    {
        return toAjax(etlDrawDwService.updateEtlDrawDw(etlDrawDw));
    }

    /**
     * 删除标准仓库
     */
    @RequiresPermissions("etl:dw:remove")
    @Log(title = "标准仓库", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(etlDrawDwService.deleteEtlDrawDwByIdDws(ids));
    }

    @GetMapping("/selectByTemplateId")
    @ResponseBody
    public TableDataInfo selectByTemplateId (Integer templateId) {
        List<EtlDrawDw> list = etlDrawDwService.selectEtlDrawDwByIdTemplate(templateId);
        return getDataTable(list);
    }

    @PostMapping("/createTable")
    @ResponseBody
    public AjaxResult createTable (@RequestBody TableCreatParamVO vo) throws Exception {
        EtlDrawDw dw = etlDrawDwService.selectEtlDrawDwByIdDw(vo.getIdBus());
        if(dw.getFgTbCre()==1) {
            return AjaxResult.error("勿重复创建");
        } else {
            DDLUtil.exec(vo.getSqlCreat());
            dw.setFgTbCre(1);
            etlDrawDwService.updateEtlDrawDw(dw);
        }
        return AjaxResult.success("创建成功");
    }

    @GetMapping("/generateCreateTableSql")
    @ResponseBody
    public AjaxResult generateCreateTableSql (Long idDw) throws Exception {
        EtlDrawDw dw = etlDrawDwService.selectEtlDrawDwByIdDw(idDw);
        String sqlCre = DDLUtil.getCreateTableSql(dw.getSqlQry(),dw.getCdTb(),null,null);
        return AjaxResult.success(sqlCre);
    }

    @PostMapping("/dwSQlValid")
    @ResponseBody
    public AjaxResult dwSQlValid (@RequestBody EtlDrawDw etlDrawDw) throws SQLException {
        return etlDrawDwService.dwSQlValid(etlDrawDw.getSqlQry());
    }

    @Autowired
    IMdMedPubfldService mdMedPubfldService;
    @GetMapping("/table/flds/diff")
    public String tableFldsDiff (Long idDw, ModelMap mmap) throws SQLException {
        EtlDrawDw etlDrawDw = etlDrawDwService.selectEtlDrawDwByIdDw(idDw);
        List<TableFieldVO> sourceFlds = DDLUtil.getColumnsFromSqlResult(etlDrawDw.getSqlQry(),null);
        List<TableFieldVO> tarFlds = DDLUtil.getColumnsFromSqlResult(DDLUtil.tableChangeToQuery(etlDrawDw.getCdTb()),null);
        Map<String, List<TableFieldVO>> map =  DDLUtil.compareFields(sourceFlds,tarFlds);
        List<TableFieldDiffVO> diffList = new ArrayList<>();
        DDLUtil.changeToDiffVO(diffList,map.get("same"),"0","不变");
        DDLUtil.changeToDiffVO(diffList,map.get("onlyInSource"),"1","新增");
        DDLUtil.changeToDiffVO(diffList,map.get("onlyInTarget"),"2","待删");
        Map<String,TableFieldDiffVO> diffMap = diffList.stream().collect(Collectors.toMap(TableFieldDiffVO::getName, e->e));
        List<MdMedPubfld> pubflds = mdMedPubfldService.findByIdList(new ArrayList<>(diffMap.keySet()));
        for (MdMedPubfld pubfld : pubflds) {
            diffMap.get(pubfld.getIdPubfld()).setFieldDes(pubfld.getNa());
        }
        mmap.put("data",diffList);
        return "etl/table/table-fld-diff";
    }
}
